package asionreachel.cn.smarthomedemo.widget;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import asionreachel.cn.smarthomedemo.utils.AppUtils;
import asionreachel.cn.smarthomedemo.utils.Mlog;


/**
 * Created by Administrator on 2016/11/17.
 * 手机验证码
 */

public class ValidatePhoneView extends TextView implements View.OnClickListener {
    public ValidatePhoneView(Context context) {
        this(context,null);
    }

    public ValidatePhoneView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ValidatePhoneView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnClickListener(this);
    }

    private long lenght = 60 * 1000;// 倒计时长度,这里给了默认60秒
    private String textafter = "秒后获取";
    private String textbefore = "获取验证码";
    private final String TIME = "time";
    private final String CTIME = "ctime";
    private OnClickListener mOnclickListener;
    private Timer t;
    private TimerTask tt;
    private long time;

    private long clickTime;

    HashMap<String, Long> map = new HashMap<String, Long>();


    private Handler han = new Handler() {
        public void handleMessage(android.os.Message msg) {
            ValidatePhoneView.this.setText(time / 1000 + textafter);
            time -= 1000;
            if (time < 0) {
                ValidatePhoneView.this.setEnabled(true);
                ValidatePhoneView.this.setText(textbefore);
                clearTimer();
            }
        }

        ;
    };

    private void initTimer() {
        time = lenght;
        t = new Timer();
        tt = new TimerTask() {

            @Override
            public void run() {
                Mlog.e(time / 1000 + "");
                han.sendEmptyMessage(0x01);
            }
        };
    }

    private void clearTimer() {
        if (tt != null) {
            tt.cancel();
            tt = null;
        }
        if (t != null)
            t.cancel();
        t = null;
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        if (l instanceof ValidatePhoneView) {
            super.setOnClickListener(l);
        } else
            this.mOnclickListener = l;
    }

    @Override
    public void onClick(View v) {
        Mlog.e("点击1");
        if((System.currentTimeMillis()-clickTime)<=300){
            return;
        }
        clickTime = System.currentTimeMillis();
        Mlog.e("点击2");
        if (mOnclickListener != null)
            mOnclickListener.onClick(v);

        // t.scheduleAtFixedRate(task, delay, period);
    }

    public void onStart(){
        time = 0;
        t = null;
        tt = null;
        initTimer();
        this.setText(time / 1000 + textafter);
        this.setEnabled(false);
        t.schedule(tt, 0, 1000);
    }

    /**
     * 和activity的onDestroy()方法同步
     */
    public void onDestroy() {
        if (AppUtils.phoneMap  == null)
        AppUtils.phoneMap = new HashMap<String, Long>();
        AppUtils.phoneMap.put(TIME, time);
        AppUtils.phoneMap.put(CTIME, System.currentTimeMillis());
        clearTimer();
        Mlog.e("onDestroy");
    }

    /**
     * 和activity的onCreate()方法同步
     */
    public void onCreate(Bundle bundle) {
        Log.e("yung", AppUtils.phoneMap + "");

        AppUtils.phoneMap = null; //统一跟IOS一致，时间不做静态保存

        if (AppUtils.phoneMap == null)
            return;
        if (AppUtils.phoneMap.size() <= 0)// 这里表示没有上次未完成的计时
            return;
        long time = System.currentTimeMillis() - AppUtils.phoneMap.get(CTIME)
                - AppUtils.phoneMap.get(TIME);
        AppUtils.phoneMap.clear();
        if (time > 0)
            return;
        else {
            initTimer();
            this.time = Math.abs(time);
            t.schedule(tt, 0, 1000);
            this.setText(time + textafter);
            this.setEnabled(false);
        }
    }

}
